import 'package:backend.core/core/domain/entities/api/web/yookassa/api_yookassa_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/api/web/yookassa/api_yookassa_transaction_revocation_info.dart';
import 'package:backend.core/core/domain/entities/web/yookassa/yookassa_purchased_subscription_transaction.dart';
import 'package:backend.purchases/domain/helpers/api/create_api_money_amount_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/api/web/create_api_web_purchased_subscription_transaction_offer_type_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/api/web/yookassa/create_api_yookassa_transaction_revocation_info_from_internal_one.dart';

abstract class CreateApiYookassaPurchasedSubscriptionTransactionFromInternalOne {
  ApiYookassaPurchasedSubscriptionTransaction call({
    required YookassaPurchasedSubscriptionTransaction transaction,
    required DateTime expirationTime,
  });
}

class CreateApiYookassaPurchasedSubscriptionTransactionFromInternalOneImpl
    implements CreateApiYookassaPurchasedSubscriptionTransactionFromInternalOne {
  const CreateApiYookassaPurchasedSubscriptionTransactionFromInternalOneImpl({
    required CreateApiMoneyAmountFromInternalOne createApiMoneyAmountFromInternalOne,
    required CreateApiYookassaTransactionRevocationInfoFromInternalOne
        createApiYookassaTransactionRevocationInfoFromInternalOne,
    required CreateApiWebPurchasedSubscriptionTransactionOfferTypeFromInternalOne
        createApiWebPurchasedSubscriptionTransactionOfferTypeFromInternalOne,
  })  : _createApiMoneyAmountFromInternalOne = createApiMoneyAmountFromInternalOne,
        _createApiYookassaTransactionRevocationInfoFromInternalOne =
            createApiYookassaTransactionRevocationInfoFromInternalOne,
        _createApiWebPurchasedSubscriptionTransactionOfferTypeFromInternalOne =
            createApiWebPurchasedSubscriptionTransactionOfferTypeFromInternalOne;

  final CreateApiMoneyAmountFromInternalOne _createApiMoneyAmountFromInternalOne;
  final CreateApiYookassaTransactionRevocationInfoFromInternalOne
      _createApiYookassaTransactionRevocationInfoFromInternalOne;
  final CreateApiWebPurchasedSubscriptionTransactionOfferTypeFromInternalOne
      _createApiWebPurchasedSubscriptionTransactionOfferTypeFromInternalOne;

  @override
  ApiYookassaPurchasedSubscriptionTransaction call({
    required YookassaPurchasedSubscriptionTransaction transaction,
    required DateTime expirationTime,
  }) {
    final apiAmount = _createApiMoneyAmountFromInternalOne(
      moneyAmount: transaction.amount,
    );

    final apiOfferType = _createApiWebPurchasedSubscriptionTransactionOfferTypeFromInternalOne(
      offerType: transaction.offerType,
    );

    ApiYookassaTransactionRevocationInfo? apiRevocationInfo;

    if (transaction.revocationInfo != null) {
      apiRevocationInfo = _createApiYookassaTransactionRevocationInfoFromInternalOne(
        revocationInfo: transaction.revocationInfo!,
      );
    }

    final apiIncomeAmount = _createApiMoneyAmountFromInternalOne(
      moneyAmount: transaction.incomeAmount,
    );

    final apiTransaction = ApiYookassaPurchasedSubscriptionTransaction(
      id: transaction.id,
      amount: apiAmount,
      offerType: apiOfferType,
      time: transaction.time,
      revocationInfo: apiRevocationInfo,
      incomeAmount: apiIncomeAmount,
      expirationTime: expirationTime,
      originalTransactionId: transaction.originalTransactionId,
    );

    return apiTransaction;
  }
}
